package me.qingji.zebra.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import me.qingji.zebra.Fetcher;
import me.qingji.zebra.bean.Article;
import me.qingji.zebra.bean.Chapter;
import me.qingji.zebra.bean.Link;
import me.qingji.zebra.parser.ChapterParser;
import me.qingji.zebra.parser.TextParser;

public class ArticleCrawler {
	
	private ChapterParser chapterParser;
	private TextParser textParser;
	
	
	public ArticleCrawler(ChapterParser chapterParser, TextParser textParser) {
		this.chapterParser = chapterParser;
		this.textParser = textParser;
	}



	public Article crawl(Fetcher fetcher, Link link) {
		Article article = new Article();
		System.out.println(link.getName());
		article.setName(link.getName());
		String articleHtml = fetcher.doGet(link.getUrl());
		// 采集、解析章节列表
		article.setLink(link);
		List<Chapter> chapters = new ArrayList<Chapter>();
		Set<Link> links = chapterParser.parse(articleHtml, link.getUrl());

		for (Link chapterLink : links) {
			Chapter chapter = new Chapter();
			// 采集、解析正文
			String textHtml = fetcher.doGet(chapterLink.getUrl());
			
			chapter.setLink(chapterLink);
			chapter.setText(textParser.parse(textHtml, chapterLink.getUrl()));
			chapters.add(chapter);
			System.out.println("\t" + chapter.getLink());
//			System.out.println("\t\t" + chapter.getText());
		}
		article.setChapters(chapters);
		return article;
	}
}
